package com.shuoyue.ycgk.ui.questionbank.base;

import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.entity.QuestionParent;
import com.shuoyue.ycgk.utils.MyRichTextHelper;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.zzhoujay.richtext.RichText;

import butterknife.BindView;

public abstract class BaseMateriFragment
        extends BaseMvpFragment {
    QuestionParent questionParent;
    @BindView(R.id.marial)
    TextView marial;
    @BindView(R.id.drag)
    ImageButton drag;
    @BindView(R.id.viewpager)
    ViewPager viewPager;
    @BindView(R.id.lay_bt)
    LinearLayout layBt;
    BaseSimpleAdapter questionFragmentAdapter;
    BaseQuestionActivity activity;
    //    QuestionSimple currentQuestionSimple;
    int totalCount;
    @BindView(R.id.lay_group)
    LinearLayout layGroup;

    public GestureDetector gesture; //手势识别
    Class tcLass;
    Class kClass;

    public abstract Class getClassT();

    public abstract Class getClassK();

//    public static BaseMateriFragment getInstance(QuestionParent questionParent, int total) {
//        Bundle bundle = new Bundle();
//        bundle.putSerializable("question", questionParent);
//        bundle.putInt("count", total);
//        BaseMateriFragment questionMateriFragment = new BaseMateriFragment();
//        questionMateriFragment.setArguments(bundle);
//        return questionMateriFragment;
//    }

    public void init(QuestionParent questionParent, int total) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("question", questionParent);
        bundle.putInt("count", total);
        setArguments(bundle);
    }

    @Override
    public void initParams(Bundle bundle) {
        activity = (BaseQuestionActivity) getActivity();
        questionParent = (QuestionParent) bundle.getSerializable("question");
        totalCount = bundle.getInt("count");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_question_materi;
    }

    @Override
    protected void initData() {
        this.tcLass = getClassT();
        this.kClass = getClassK();
        marial.setTextSize(TypedValue.COMPLEX_UNIT_SP, questionParent.getTextSize());
        MyRichTextHelper.showRich(mContext,questionParent.getMaterialContent(),marial);

        gesture = new GestureDetector(this.getActivity(), new MyOnGestureListener());
        //为fragment添加OnTouchListener监听器
        mContentView.setOnTouchListener((v, event) -> {
            return gesture.onTouchEvent(event);//返回手势识别触发的事件
        });


        questionFragmentAdapter = new BaseSimpleAdapter(getChildFragmentManager(),
                questionParent.getQuestionSimpleDTOS(), totalCount, tcLass, kClass);
        viewPager.setAdapter(questionFragmentAdapter);

    }

    @Override
    protected void initView() {

    }


    /**
     * 跳转下一题
     */
    public void goNext() {
        if (viewPager.getCurrentItem() == questionFragmentAdapter.getCount() - 1) {
            //最后一个
            activity.goNext();
        } else {
            viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
        }
    }



    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if ((isVisibleToUser && isResumed())) {
            onResume();
        } else if (!isVisibleToUser) {
            onPause();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (getUserVisibleHint()) {
            resetTextSize();
            //TODO give the signal that the fragment is visible
//            viewPager.setCurrentItem(0);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        //TODO give the signal that the fragment is invisible
    }

    //设置手势识别监听器
    private class MyOnGestureListener extends GestureDetector.SimpleOnGestureListener {
        boolean isBeginInDrag = false;//起点是否在拖动控件内

        int height = 0;
        int minHeight = 300;
        int maxHeight = 0;

        @Override//此方法必须重写且返回真，否则onFling不起效
        public boolean onDown(MotionEvent e) {
            Log.e("onDown", e.getRawY() + "");

            int dragH = SizeUtil.getLocationH(drag);//控件高度
            int layButtomBegin = SizeUtil.getLocationH(layBt);
            int layMateriBegin = SizeUtil.getLocationH(marial);
            height = layButtomBegin - layMateriBegin;
            int screenH = SizeUtil.getScreenHeight(mContext);
            maxHeight = screenH - layMateriBegin - dragH - 50;

            //设置判断是否有效的范围
            int judgBegin = dragH - 40;//控件上40像素
            int judgEnd = dragH + drag.getHeight() + 40;//控件下40像素

            if (e.getRawY() < judgBegin || e.getRawY() > judgEnd) {
                //范围之外,拦截事件
                isBeginInDrag = false;
                return false;
            } else {
                isBeginInDrag = true;
            }

            return true;
        }


        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if (isBeginInDrag) {
                //在范围内
                //根据滑动距离修改顶部空间高度
                int heightNew = (int) (height + e2.getRawY() - e1.getRawY());
                Log.e("heightNew", heightNew + "");
                if (heightNew < minHeight && e1.getRawY() > e2.getRawY()
                        || heightNew > maxHeight && e1.getRawY() < e2.getRawY()) {

                } else {
                    LinearLayout.LayoutParams layoutParams
                            = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                            heightNew);
                    marial.setLayoutParams(layoutParams);
                }
            }
            return super.onScroll(e1, e2, distanceX, distanceY);
        }
    }

    public void goIndex(int index) {
        viewPager.setCurrentItem(index);
    }


    public void resetTextSize() {
        if (marial != null && questionParent != null) {
            marial.setTextSize(TypedValue.COMPLEX_UNIT_SP, questionParent.getTextSize());
        }
            if(questionFragmentAdapter!=null
                    &&questionFragmentAdapter.getCurrentFragment()!=null){
                BaseSimpleFragment baseSimpleFragment = (BaseSimpleFragment) questionFragmentAdapter.getCurrentFragment();
                baseSimpleFragment.resetTextSize();
            }
    }
}
